package com.best.weiyi.util;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.best.weiyi.entity.Music;

public class MusicUtil {
	public static ArrayList<Music> getMusicData(Context context){
		ArrayList<Music> music = new ArrayList<Music>();
		ContentResolver cr = context.getContentResolver();
		if(cr != null){
			Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.DURATION+">=?",
					new String[] {"120000"}, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			if(null == cursor){
				return null;
			}
			while(cursor.moveToNext()){
				Music m = new Music();
				//标题
				m.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
				//名称
				m.setName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
				//歌手
				String singer = (cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
				if(null==singer || "".equals(singer) || "<unknown>".equals(singer)){
					singer = "未知歌手";
				}
				m.setSinger(singer);
				
				m.setAlbum(cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
				//大小
				m.setSize(cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.SIZE)));
				//时间
				m.setTime(cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.DURATION)));
				//歌曲地址
				m.setUrl(cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATA)));
				//歌词地址
				System.out.println(m);
				music.add(m);
				
			}
			cursor.close();

		}

		return music;
		
	}
	/**
	 * 时间格式转换
	 * @param time
	 * @return
	 */
	public static String toTime(int time) {

		time /= 1000;
		int minute = time / 60;
		int hour = minute / 60;
		int second = time % 60;
		minute %= 60;
		return String.format("%02d:%02d", minute, second);
	}
}
